"""
通知相关的Pydantic模式
@deprecated: 部分Schema已废弃，请使用简化的NotifyLog相关Schema
"""

from pydantic import BaseModel, Field
from typing import Optional, Dict, Any, List
from datetime import datetime
from decimal import Decimal
from enum import Enum


class NotificationType(str, Enum):
    """通知类型枚举"""
    PRODUCT_FOUND = "product_found"
    MONITOR_STARTED = "monitor_started"
    MONITOR_EXPIRED = "monitor_expired"
    BALANCE_LOW = "balance_low"
    SYSTEM_MESSAGE = "system_message"


class NotificationChannel(str, Enum):
    """通知渠道枚举"""
    EMAIL = "email"
    WECHAT = "wechat"
    WEBSOCKET = "websocket"
    SMS = "sms"


class NotificationStatus(str, Enum):
    """通知状态枚举"""
    PENDING = "pending"
    SENT = "sent"
    DELIVERED = "delivered"
    FAILED = "failed"
    READ = "read"


class NotificationCreate(BaseModel):
    """通知创建模式"""
    notification_type: NotificationType = Field(..., description="通知类型")
    title: str = Field(..., max_length=200, description="通知标题")
    content: str = Field(..., description="通知内容")
    channel: NotificationChannel = Field(..., description="通知渠道")
    recipient: str = Field(..., description="接收者")
    data: Optional[Dict[str, Any]] = Field(None, description="扩展数据")
    priority: Optional[int] = Field(5, ge=1, le=10, description="优先级")
    expires_at: Optional[datetime] = Field(None, description="过期时间")


class NotificationResponse(BaseModel):
    """通知响应模式"""
    id: int = Field(..., description="通知ID")
    user_id: int = Field(..., description="用户ID")
    monitor_id: Optional[int] = Field(None, description="关联监控任务ID")
    notification_type: NotificationType = Field(..., description="通知类型")
    title: str = Field(..., description="通知标题")
    content: str = Field(..., description="通知内容")
    channel: NotificationChannel = Field(..., description="通知渠道")
    status: NotificationStatus = Field(..., description="通知状态")
    data: Optional[Dict[str, Any]] = Field(None, description="扩展数据")
    recipient: str = Field(..., description="接收者")
    sender: Optional[str] = Field(None, description="发送者")
    
    # 时间信息
    created_at: datetime = Field(..., description="创建时间")
    sent_at: Optional[datetime] = Field(None, description="发送时间")
    delivered_at: Optional[datetime] = Field(None, description="送达时间")
    read_at: Optional[datetime] = Field(None, description="阅读时间")
    expires_at: Optional[datetime] = Field(None, description="过期时间")
    
    # 错误信息
    error_message: Optional[str] = Field(None, description="错误信息")
    retry_count: int = Field(..., description="重试次数")
    
    # 状态标识
    is_expired: bool = Field(..., description="是否已过期")
    can_retry: bool = Field(..., description="是否可以重试")
    
    class Config:
        from_attributes = True


class ProductNotificationResponse(BaseModel):
    """商品推送记录响应模式"""
    id: int = Field(..., description="推送记录ID")
    user_id: int = Field(..., description="用户ID")
    monitor_id: int = Field(..., description="监控任务ID")
    
    # 商品信息
    product_external_id: str = Field(..., description="外部商品ID")
    product_title: str = Field(..., description="商品标题")
    product_price: Optional[Decimal] = Field(None, description="商品价格")
    product_currency: str = Field(..., description="货币单位")
    seller_name: Optional[str] = Field(None, description="卖家名称")
    product_url: Optional[str] = Field(None, description="商品链接")
    product_image_url: Optional[str] = Field(None, description="商品主图")
    product_location: Optional[str] = Field(None, description="商品所在地")
    
    # 匹配信息
    matched_keyword: Optional[str] = Field(None, description="匹配的关键词")
    matched_seller: Optional[str] = Field(None, description="匹配的卖家")
    match_type: str = Field(..., description="匹配类型")
    
    # 推送状态
    notification_sent: bool = Field(..., description="是否已发送通知")
    notification_channel: Optional[str] = Field(None, description="通知渠道")
    
    # 用户交互
    is_clicked: bool = Field(..., description="用户是否点击")
    is_interested: Optional[bool] = Field(None, description="用户是否感兴趣")
    user_feedback: Optional[str] = Field(None, description="用户反馈")
    
    # 数据来源
    source_platform: str = Field(..., description="数据来源平台")
    crawl_timestamp: datetime = Field(..., description="爬取时间")
    created_at: datetime = Field(..., description="创建时间")
    
    class Config:
        from_attributes = True


class NotificationStats(BaseModel):
    """通知统计模式"""
    user_id: int = Field(..., description="用户ID")
    total_notifications: int = Field(..., description="总通知数")
    unread_notifications: int = Field(..., description="未读通知数")
    
    # 按类型统计
    product_notifications: int = Field(..., description="商品通知数")
    system_notifications: int = Field(..., description="系统通知数")
    
    # 按状态统计
    sent_notifications: int = Field(..., description="已发送通知数")
    failed_notifications: int = Field(..., description="发送失败通知数")
    
    # 按渠道统计
    email_notifications: int = Field(..., description="邮件通知数")
    wechat_notifications: int = Field(..., description="微信通知数")
    
    # 用户交互统计
    clicked_notifications: int = Field(..., description="点击通知数")
    click_rate: float = Field(..., description="点击率（%）")
    
    # 时间统计
    today_notifications: int = Field(..., description="今日通知数")
    this_week_notifications: int = Field(..., description="本周通知数")
    last_notification_at: Optional[datetime] = Field(None, description="最后通知时间")


class NotificationListResponse(BaseModel):
    """通知列表响应模式"""
    notifications: List[NotificationResponse] = Field(..., description="通知列表")
    total: int = Field(..., description="总数量")
    unread_count: int = Field(..., description="未读数量")
    has_more: bool = Field(..., description="是否有更多")


class ProductNotificationListResponse(BaseModel):
    """商品推送记录列表响应模式"""
    notifications: List[ProductNotificationResponse] = Field(..., description="推送记录列表")
    total: int = Field(..., description="总数量")
    clicked_count: int = Field(..., description="点击数量")
    interested_count: int = Field(..., description="感兴趣数量")
    has_more: bool = Field(..., description="是否有更多")


class NotificationMarkRequest(BaseModel):
    """通知标记请求模式"""
    notification_ids: List[int] = Field(..., description="通知ID列表")
    action: str = Field(..., description="操作类型: read/unread/delete")


class UserFeedbackRequest(BaseModel):
    """用户反馈请求模式"""
    product_notification_id: int = Field(..., description="商品推送记录ID")
    is_interested: bool = Field(..., description="是否感兴趣")
    feedback: Optional[str] = Field(None, max_length=500, description="用户反馈")
